package com.badger.generator.pattern;

import com.badger.generator.constant.DataBaseDriver;
import com.badger.generator.convert.MySqlTypeConvert;
import com.badger.generator.convert.OracleTypeConvert;
import com.badger.generator.convert.PostgreSqlTypeConvert;
import com.badger.generator.convert.TypeConvert;
import com.badger.generator.entity.GeneratorAttr;

/**
 * 类型转换，类型校验的单例工厂
 * @author liqi
 *
 */
public final class ConvertSingleFactory {

    private static ConvertSingleFactory FACTORY;

    private ConvertSingleFactory() {
    }

    public static ConvertSingleFactory getInstance() {
        if (null == FACTORY) {
            synchronized (ConvertSingleFactory.class) {
                if (null == FACTORY) {
                    FACTORY = new ConvertSingleFactory();
                }
            }
        }
        return FACTORY;
    }

    public TypeConvert build(String driver) {
        TypeConvert type;
        switch (driver) {
        case DataBaseDriver.MYSQL:
            type = new MySqlTypeConvert();
            break;
        case DataBaseDriver.ORACLE:
            type = new OracleTypeConvert();
            break;
        default:
            type = new PostgreSqlTypeConvert();
            break;
        }
        return type;
    }

    public AbstractDataBaseStrategy buildDataBaseFactory(GeneratorAttr attr) {
        AbstractDataBaseStrategy factory = null;
        switch (attr.getDriver()) {
        case DataBaseDriver.MYSQL:
            factory = MysqlStrategy.getInstance(attr);
            break;
        case DataBaseDriver.ORACLE:
            factory = OracleStrategy.getInstance(attr);
            break;
        default:
            factory = PostgreSqlStrategy.getInstance(attr);
            break;
        }
        return factory;
    }
}
